﻿using Newtonsoft.Json.Linq;
using System;
using System.Linq;
using Wicture.DbRESTFul;
using Wicture.DbRESTFul.SQL;
using Wicture.EPC.OnlineQueryProxy.Agents;
using Wicture.EPC.OnlineQueryProxy.Proxy;

namespace Wicture.EPC.OnlineQueryProxy.Repositories
{
    public class QueryProxyRepository : DbRESTFulRepository
    {
        private readonly IOnlineQueryRequestHandler _onlineQueryRequestHandler;
        private readonly IAgentManager _agentManager;

        public QueryProxyRepository(IOnlineQueryRequestHandler onlineQueryRequestHandler, IAgentManager agentManager)
        {
            _onlineQueryRequestHandler = onlineQueryRequestHandler;
            _agentManager = agentManager;
        }

        public object VinQuery(JToken param)
        {
            return _onlineQueryRequestHandler.Query(new QueryRequest
            {
                Task = Processors.QueryProcessorTask.Vin,
                ParamString = param.ToString()
            });
        }

        public object PartPriceQuery(JToken param)
        {
            return _onlineQueryRequestHandler.Query(new QueryRequest
            {
                Task = Processors.QueryProcessorTask.PartPrice,
                ParamString = param.ToString()
            });
        }

        public object QueryPriceAndStock(JToken param)
        {
            return _onlineQueryRequestHandler.Query(new QueryRequest
            {
                Task = Processors.QueryProcessorTask.PartPriceAndStock,
                ParamString = param.ToString()
            });
        }

        public object QueryAgentStatus(JToken param)
        {
            var processorName = param.Value<string>("processorName");

            if (string.IsNullOrEmpty(processorName))
            {
                return AgentCollection.Agents;
            }
            else
            {
                return AgentCollection.Agents
                        .Where(a => a.ProcessorName.Equals(processorName, StringComparison.InvariantCultureIgnoreCase));
            }
        }

        public object ToggleAgent(JToken param)
        {
            var agentId = param.Value<string>("agentId");

            var agent = AgentCollection.Agents
                .FirstOrDefault(a => a.Id.Equals(agentId, StringComparison.InvariantCultureIgnoreCase));

            if (agent == null)
            {
                throw new LogicalException($"No agent found by agentId：{agentId}.");
            }

            agent.Enabled = !agent.Enabled;

            return true;
        }

        public object RefreshAgentStatus(JToken param)
        {
            var processorName = param.Value<string>("processorName");
            _onlineQueryRequestHandler.RefreshAgentStatus(processorName);
            return true;
        }

        public object QueryAgentConfigs(JToken param)
        {
            var processorName = param.Value<string>("processorName");
            if (string.IsNullOrEmpty(processorName))
            {
                return base.Invoke("List_agent_config", new object { });
            }
            else
            {
                return base.Invoke("Search_agent_config", new { processorName });
            }
        }
    }
}
